/**
 * jvs
 * 
 * @author liuguojun
 * @date 2015-09-17
 */
var jvs = {

	settings : {
		service : 'http://wss.56daily.cn'
	},

	init : function() {

		var _self = this;

		_self.getServerToken();
        
        jvs.page.init();
        jvs.article.init();
        jvs.order.init();
        jvs.index.init();
	},

	getServerToken : function() {
		$('body').append('<script src="' + jvs.settings.service + '/getServerToken.action?slink=' + escape(location.href) + '" type="text/javascript"></script>');
	},

};

/**
 * jvs核心
 */
jvs.core = {

	alert : function(s) {
		alert(s);
	},

	confirm : function(s) {
		return confirm(s);
	},

	log : function(s) {
		if (console) {
			console.log(s);
		}
	},

	getURLParams : function(key) {
		var url = location.href;
		var param = '';

		if (url.indexOf('?') > -1) {
			param = url.substring(url.indexOf('?') + 1);
		}
		if (!!param && !!key) {
			var idx1 = param.indexOf(key + '=');
			if (idx1 > -1) {
				param = param.substring(idx1 + key.length + 1);

				var idx2 = param.indexOf('&')
				if (idx2 > -1) {
					param = param.substring(0, idx2);
				}
			} else {
				param = '';
			}
		}

		return param;
	}
};

/**
 * jvs资讯
 */
jvs.page = {

    init: function(){
        var _self = this;
        _self.loadNavigation();
        _self.loadAside();
        _self.loadJiaThis();
    },

    loadNavigation: function(){
        var html = $.trim($('.main-navigation').text());
        if (!html) {
            $(".main-navigation").load("/includes/navigation.html");
        }
    },
    
    loadAside: function(){
        var html = $.trim($('aside').text());
        if (!html) {
            $("aside").load("/includes/side.html", function(){
                jvs.index.init();
            });
        }
    },
    
    loadJiaThis: function(){
        if ($('.post-content').length == 1) {
            $.get("/includes/jiathis.html", function(result){
                $('.post-content').append(result);
            });
        }
    }
};

/**
 * jvs资讯
 */
jvs.article = {

    init: function(){
        var _self = this;
        
        if ($('#moreButton').length == 1){
	        $('#moreButton').click(function(){
	            var params = $(this).attr('data-params');
	           // var cpage = $(this).attr('data-cpage');
	           // cpage = parseInt(cpage, 10) + 1;
	           // params += "&cpage=" + cpage;
	            
	            var str = $('article:last').find('.post-title a').attr('href'); 
	            str = str.substring(str.lastIndexOf('/') + 1); 
	            str = str.substring(0, str.lastIndexOf('.'));
	            params += "&caid=" + str;
	            
	            _self.query(params);
	        });
	    }
	    
	    _self.statisticsArticle();
    },

    query : function(s) {
        var _self = this;
		$.ajax({
			url: jvs.settings.service + '/cms/queryArticle.action?' + s,  
			dataType: 'jsonp',  
			jsonp: 'callback',  
			success:function(result) {
			    $('#moreButton').attr('data-cpage', result.data.curpage);
				$(result.data.data).each(function(idx, obj){
				    $('#moreButton').before(_self.printItem(obj));
				});
			},
			timeout:3000  
		});
	},
	
    printItem: function(obj){
        var _self = this;
        var html = '';
        html += '<article class="post">';
        html += '<div class="post-head">';
        html += '<h1 class="post-title"><a href="' + _self.getLink(obj) + '">' + obj.title + '</a></h1>';
        html += '<div class="post-meta">';
        html += '<span class="author">来源：' + obj.source + '</span> &bull;';
        html += '<time class="post-date" datetime="' + obj.ctime + '" title="' + obj.ctime + '">' + obj.ctime + '</time>';
        html += '</div>';
        html += '</div>';
        if (obj.picture.length > 1) {
            html += '<div class="featured-media">';
            html += '<a href="' + _self.getLink(obj) + '"><img src="' + obj.picture + '" alt="' + obj.title + '"></a>';
            html += '</div>';
        }
        html += '<div class="post-content">';
        html += '<p>' + obj.summary + '</p>';
        html += '</div>';
        html += '<div class="post-permalink">';
        html += '<a href="' + _self.getLink(obj) + '" class="btn btn-default">阅读全文</a>';
        html += '</div>';
        html += '</article>';
        
        return html;
    },
    
    statisticsArticle: function(){
        var link = location.href;
        if (link.lastIndexOf('/') > -1) {
            link = link.substring(link.lastIndexOf('/') + 1);
        }
        if (link.lastIndexOf('.') > -1) {
            link = link.substring(0, link.lastIndexOf('.'));
        }
        if (parseInt(link)) {
            var param = {'caid': link, 'flag': 11};
            $.ajax({
	            url: jvs.settings.service + '/cms/modifyArticleReport.action',
	            dataType: 'jsonp',
	            data : param,
	            jsonp: 'callback',
	            success:function(result) {},
	            timeout:3000  
	        });
        }
        
    },
	
    getLink: function(obj){
        return obj.apath.replace('/'+obj.scid, '').replace('.article', '.html');
    }

};


/**
 * jvs订单
 */
jvs.order = {

    init: function(){
        var _self = this;
        
        if ($('#searchOrder').length == 1) {
	        $('#searchOrder').click(function(){
	            var tiid = $.trim($('#tiid').val());
	            if (!!tiid) {
	                _self.query({'tiid': tiid});
	                
	                $('#orderStatus').html('<div class="row text-center"><img src="./images/loading.gif" width="100"></div>');
	            }
	        });
        }
    },
    
    query: function(param){
        var _self = this;
        
        $.ajax( {
            url : jvs.settings.service + '/tms/queryInvoiceLog.action',
            dataType : 'jsonp',
            data : param,
            jsonp : 'callback',
            success : function(result) {
                if (!!result){
                    if (result.code == '0000') {
                        $('#orderStatus').html('');
                        $.each(result.data.data, function(i, orderStatus){
                            $('#orderStatus').append(_self.printStatus(i, orderStatus));
                        });
                    } else {
                        $('#orderStatus').html(result.data);    
                    }
                } else {
                    $('#orderStatus').html('服务访问异常');
                }
            },
            error : function(){
                $('#orderStatus').html('网络异常');
            }
        });
    },
    
    printStatus: function(index, order){
        var html = '';
        if (index > 0) {
            html += '<div class="row text-center"><span class="glyphicon glyphicon-arrow-up"></span></div>';
        } else {
            html += '<hr />';
        }
        html += '<div class="row">';
        html += '<div class="col-xs-4 text-right">';
        html += order.ctime;
        html += '</div>';
        html += '<div class="col-xs-8">';
        html += order.content;
        html += '</div>';
        html += '</div>';
                    
        return html;
    }
};

/**
 * 物流指数
 */
jvs.index = {

    init: function(){
        var _self = this;
        
        _self.loadLPI();
        _self.loadWI();
        _self.loadELI();
    },
    
    loadLPI: function(){
        var indexLPIEle = document.getElementById('indexLPI');
        if (!indexLPIEle) {
            return ;
        }
        var lpiChart = echarts.init(indexLPIEle);

        // 指定图表的配置项和数据
        var option = {
            tooltip: {},
            legend: {
                data:['LPI']
            },
            xAxis: {
                data: [
                "2013-10","2013-11","2013-12",
                "2014-1","2014-2","2014-3","2014-4","2014-5","2014-6","2014-7","2014-8","2014-9","2014-10","2014-11","2014-12",
                "2015-1","2015-2","2015-3","2015-4","2015-5","2015-6","2015-7","2015-8","2015-9","2015-10","2015-11","2015-12",
                "2016-1","2016-2","2016-3","2016-4","2016-5","2016-6","2016-7","2016-8","2016-9","2016-10","2016-11","2016-12",
                "2017-1","2017-2"
                ]
            },
            yAxis: {
                min: 0,
                max: 100
            },
            series: [
                {
                    name: 'LPI',
                    type: 'line',
                    data: [
                        53.6, 53.0, 52.4, 
                        51.5, 51.9, 53.0, 57.7, 55.2, 56.7, 56.8, 54.1, 56.4, 54.9, 56.5, 57.5,
                        56.3, 54.9, 58.0, 58.6, 58.0, 55.7, 52.2, 52.0, 52.2, 53.5, 54.2, 55.0,
                        53.3, 50.0, 52.9, 54.2, 54.2, 55.5, 54.8, 54.3, 59.0, 59.2, 59.3, 56.0,
                        52.5, 53.2
                    ]
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        lpiChart.setOption(option);
    },
    
    
    loadWI: function(){
        var indexWIEle = document.getElementById('indexWI');
        if (!indexWIEle) {
            return ;
        }
        var wiChart = echarts.init(indexWIEle);

        // 指定图表的配置项和数据
        var option = {
            tooltip: {},
            legend: {
                data:['WI']
            },
            xAxis: {
                data: [
                    "2016-1","2016-2","2016-3","2016-4","2016-5","2016-6","2016-7","2016-8","2016-9","2016-10","2016-11","2016-12",
                    "2017-1","2017-2"
                ]
            },
            yAxis: {
                min: 0,
                max: 100
            },
            series: [
                {
                    name: 'WI',
                    type: 'line',
                    data: [
                        47.7, 46.4, 53.8, 51.0, 51.2, 50.6, 53.7, 53.0, 50.6, 50.7, 54.5, 52.7,
                        51.0, 54.0
                    ]
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        wiChart.setOption(option);
    },
    
    loadELI: function(){
        var indexELIEle = document.getElementById('indexELI');
        if (!indexELIEle) {
            return ;
        }
        var eliChart = echarts.init(indexELIEle);

        // 指定图表的配置项和数据
        var option = {
            tooltip: {},
            legend: {
                data:['ELI']
            },
            xAxis: {
                data: [
                "2016-1","2016-2","2016-3","2016-4","2016-5","2016-6","2016-7","2016-8","2016-9","2016-10","2016-11","2016-12",
                "2017-1","2017-2"
                ]
            },
            yAxis: {
                min: 60,
                max: 160
            },
            series: [
                {
                    name: 'ELI',
                    type: 'line',
                    data: [
                        null, null, null, null, null, null, null, null, null, 119.0, 123.4, 121.0,
                        119.7, 124.0
                    ]
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        eliChart.setOption(option);
    }
    
};

$(document).ready(function() {
    jvs.init();
});